package com.ggs.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Date;
import java.util.Map;

public class JwtUtils {
    private static final String KEY = "ggs";
    private static final long EXPIRATION_TIME = 1000 * 60 * 60 * 24;
    private static final Algorithm ALGORITHM = Algorithm.HMAC256(KEY);

    // 接收业务数据，生成token并返回
    public static String genToken(Map<String, Object> claims){
        return JWT.create()
                .withClaim("claims", claims)
                .withExpiresAt(new Date(System.currentTimeMillis() + EXPIRATION_TIME))
                .sign(ALGORITHM);
    }

    // 接收token，验证业务信息并返回
    public static Map<String, Object> parseToken(String token){
        return JWT.require(ALGORITHM)
                .build()
                .verify(token)
                .getClaim("claims")
                .asMap();
    }
}
